/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.service.system.impl;

import java.util.Date;
import java.util.List;

import org.grayrabbit.cms.dao.system.OperLogDao;
import org.grayrabbit.cms.entity.OperLog;
import org.grayrabbit.cms.service.system.OperLogService;
import org.grayrabbit.cms.util.DateUtil;
import org.grayrabbit.cms.vo.system.OperLogBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.grayrabbit.dao.util.Page;
import org.grayrabbit.service.impl.GenericServiceImpl;
/**
 * <b>OperLog</b>的service实现类，事务加此处：





 * @version 1.0,创建时间: 2011-05-16 15:55:12  
 */
@Service
@Transactional
public class OperLogServiceImpl extends GenericServiceImpl implements OperLogService {
	/**
	 * 注入dao层：
	 */
	@Autowired
	@Qualifier("operLogDaoImpl")
	private OperLogDao operLogDao;
	
	/**
	 * 此方法执行时为只读事务：
	 * @see OperLogService#getOperLog(String)
	 */
	@Transactional(readOnly=true)
	public OperLog getOperLog(final String operLogId){
		OperLog operLog = operLogDao.findById(operLogId);
		operLogDao.initialize(operLog);
		return operLog;
	}
	
	/**
	 * @see OperLogService#delete(String)
	 */
	@Transactional
	public void delete(final OperLog operLog) {
		operLogDao.makeTransient(operLog);
	}
	
	/**
	 * @see OperLogService#save(OperLog)
	 */
	@Transactional
	public void save(final OperLog operLog) {
		operLogDao.makePersistent(operLog);		
	}
	
	/**
	 * 此方法执行时为只读事务：
	 * @see OperLogService#getPage(OperLogBean)
	 */
	@Transactional(readOnly=true)
	public Page<OperLog> getPage(final OperLogBean operLogBean){
		Page<OperLog> page =  new Page<OperLog>();
		page.setTotal(operLogDao.count(operLogBean));
		page.setResult(operLogDao.findByQc(operLogDao.assembly(operLogBean)));
		return page;
	}

	/**
	 * 此方法执行时为只读事务：
	 * @see OperLogService#list(Page, OperLogBean)
	 */
	public void list(Page<OperLog> page, OperLogBean operLogBean) {
		if(page==null)
			return;
		if(page.isAutoCount()){
			page.setTotal(operLogDao.count(operLogBean));
		}
		page.setResult(operLogDao.findByPage(page,operLogBean));
	}
	
	/**
	 * 根据主键id列删除，批量删除OperLog
	 * @see OperLogService#delete(String[])
	 */
	public void delete(final String[] key) {
		int i =0;
		for(String id : key){
			operLogDao.makeTransient(operLogDao.findById(id));
			i++;
			if(i==MAX_CLEAR)
				operLogDao.clear();
		}
		
	}

	/**
	 * @param admin  当前用户
	 * @param modelType 操作的模块类型
	 * @param act 操作动作
	 * @param ObjectId  操作对象ID
	 * @return boolean 添加成功 返回true
	 *  按照输入的参数预先组装MEMO 
	 */
	@Transactional
	public void addLog(String loginName, String realName ,
			String modelType,String act,String ObjectId,String ip){ 
		OperLog operLog = new OperLog();
		loadOperLogInfo(operLog,loginName, realName, modelType, act, ObjectId,ip);
		operLog.setMemo(realName+" , "+DateUtil.formatDateToString(new Date(), "yyyy-MM-dd HH:mm:ss") 
				+"在(模块) "+modelType+" 中，对ID为： "+ObjectId+" 的对象进行了 "+act+" 操作");
		operLogDao.makePersistent(operLog);	
	}
	  
	/**
	 * @param admin  当前用户
	 * @param modelType 操作的模块类型
	 * @param act 操作动作
	 * @param ObjectId  操作对象ID
	 * @param memo  自定义MEMO
	 */
	@Transactional
	public void addLog(String loginName, String realName ,
			String modelType,String act,String objectId,String memo,String ip){ 
		OperLog operLog = new OperLog();
		loadOperLogInfo(operLog,loginName, realName, modelType, act, objectId,ip);
		operLog.setMemo(memo);
		operLogDao.makePersistent(operLog);	
	}
	
	/**
	 * @param loginName
	 * @param realName
	 * @param modelType
	 * @param act
	 * @param ObjectId  
	 *  装载日志信息(memo除外)
	 */
	private void loadOperLogInfo(OperLog operLog,String loginName, 
			String realName ,String modelType,String act,String ObjectId,String ip){
		operLog.setLoginName(loginName);
		operLog.setRealName(realName);
		operLog.setOperFunc(modelType);
		operLog.setOperAct(act);
		operLog.setOperObjectId(ObjectId);
		operLog.setOperIp(ip);
		operLog.setOperTime(new Date());
	} 
	
	@Transactional(readOnly=true)
	public List<OperLog> getLogInfos( OperLogBean operLogBean) { 
		return operLogDao.getLogInfos(operLogBean);
	}
     
}